# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/10/18
# @File   : EG14_LegalRatio.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:法人股权占比/最大股东股权占比
code:EG14_LegalRatio
tableName:
columnName:
groups:企业通用模块
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        if eg in null_type_list:
            return None
        if isinstance(eg, list):
            return eg
        else:
            return None
    except:
        return None


# 统一转成datetime对象，只能转年月日的字符串
def date_format(str):
    import re
    r = re.compile(r'\D+')
    try:
        time = str[0:10]
        date = datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                except:
                    date = None
    return date


def float_check(target):
    try:
        float(target)
        return True
    except:
        return False

def EG14_LegalRatio():

    shareHolderList = FormatData('shareHolderList')
    legalPerson = ratingRequest.get("legalPerson")

    if shareHolderList is None:
        return u'缺失值'
    else:
        if len(shareHolderList) == 0:
            return 1
        else:
            fundedRatioShareHolders = []
            legalPersonRatio = None
            for i in shareHolderList:
                shareholderName = i.get("shareholderName")
                fundedRatio = i.get("fundedRatio")
                if fundedRatio in null_type_list:
                    continue
                if shareholderName == legalPerson:
                    legalPersonRatio = fundedRatio
                else:
                    fundedRatioShareHolders.append(fundedRatio)

            if legalPersonRatio is None:
                return u'缺失值'
            elif len(fundedRatioShareHolders) == 0:
                return 1
            else:
                legalPersonRatio = legalPersonRatio.replace("%", "")
                legalPersonRatio = float(legalPersonRatio) / 100.0 if float_check(legalPersonRatio) else None

                if legalPersonRatio is None:
                    return u'缺失值'
                else:
                    shareHolderMax = 0
                    for shareHolderRatio in fundedRatioShareHolders:
                        shareHolderRatio = shareHolderRatio.replace("%", "")
                        shareHolderRatio = float(shareHolderRatio) / 100.0 if float_check(
                            shareHolderRatio) else None
                        if shareHolderRatio is None:
                            continue
                        else:
                            if shareHolderRatio > shareHolderMax:
                                shareHolderMax = shareHolderRatio
                    if shareHolderMax == 0:
                        return 1
                    else:
                        shareHolderMax = max(legalPersonRatio, shareHolderMax)
                        return round(legalPersonRatio / shareHolderMax, 4)



result = EG14_LegalRatio()